package com.example.framework2002a.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.framework2002a.R;
import com.example.framework2002a.databinding.FragmentHeadlinesBinding;
import com.example.framework2002a.databinding.ItemHeadlinesBinding;
import com.example.lib_core.bean.HeadlinesBean;

import java.util.List;

public class HeadLinesAdapter extends BaseQuickAdapter<HeadlinesBean, BaseViewHolder> {

    private FragmentHeadlinesBinding headlinesBinding;

    public HeadLinesAdapter(@Nullable List<HeadlinesBean> data,FragmentHeadlinesBinding binding) {
        super(R.layout.item_headlines, data);
        this.headlinesBinding = binding;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, HeadlinesBean headlinesBean) {
        ItemHeadlinesBinding binding = DataBindingUtil.getBinding(baseViewHolder.itemView);
        if (binding != null){
            binding.setHead(headlinesBean);
            binding.executePendingBindings();
        }

        //----点赞
        binding.iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int t1 = Integer.parseInt(binding.tv1.getText().toString());
                t1++;
                binding.tv1.setText(t1+"");
            }
        });

        //----评论
        binding.iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (headlinesBinding.headCl.getVisibility() == View.GONE){
                    headlinesBinding.headCl.setVisibility(View.VISIBLE);
                }else{
                    headlinesBinding.headCl.setVisibility(View.GONE);
                }
            }
        });

        //----分享
        binding.iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected void onItemViewHolderCreated(@NonNull BaseViewHolder viewHolder, int viewType) {
        super.onItemViewHolderCreated(viewHolder, viewType);
        DataBindingUtil.bind(viewHolder.itemView);
    }

    @BindingAdapter(value = "image")
    public static void getImage(ImageView imageView, String url){
        Glide.with(imageView.getContext()).load(url).into(imageView);
    }
}
